package me.zhengjie.pm.taskassignment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.taskassignment.domain.StaffWorkTask;
import me.zhengjie.pm.taskassignment.service.StaffWorkTaskService;
import me.zhengjie.pm.taskassignment.service.dto.StaffWorkTaskQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author yuxj
* @date 2020-04-14
*/
@Api(tags = "任务分配管理")
@RestController
@RequestMapping("/api/staffWorkTask")
public class StaffWorkTaskController {

    private final StaffWorkTaskService staffWorkTaskService;

    public StaffWorkTaskController(StaffWorkTaskService staffWorkTaskService) {
        this.staffWorkTaskService = staffWorkTaskService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('staffWorkTask:list')")
    public void download(HttpServletResponse response, StaffWorkTaskQueryCriteria criteria) throws IOException {
        staffWorkTaskService.download(staffWorkTaskService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询任务分配")
    @ApiOperation("查询任务分配")
    @PreAuthorize("@el.check('staffWorkTask:list')")
    public ResponseEntity<Object> getStaffWorkTasks(StaffWorkTaskQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(staffWorkTaskService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增任务分配")
    @ApiOperation("新增任务分配")
    @PreAuthorize("@el.check('staffWorkTask:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody StaffWorkTask resources){
        return new ResponseEntity<>(staffWorkTaskService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改任务分配")
    @ApiOperation("修改任务分配")
    @PreAuthorize("@el.check('staffWorkTask:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody StaffWorkTask resources){
        staffWorkTaskService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除任务分配")
    @ApiOperation("删除任务分配")
    @PreAuthorize("@el.check('staffWorkTask:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        staffWorkTaskService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}